<!DOCTYPE html>
<html>
<head>
  <title>Test - Delegates</title>
  <link rel="stylesheet" href="../QUnit.css" type="text/css" />
  <script type="text/javascript" src="../QUnit.js"></script>
  <script type="text/javascript" src="../QUnitExt.js"></script>
</head>
<body>
  <h1 id="qunit-header">Test Results</h1>
  <h2 id="qunit-banner"></h2>
  <h2 id="qunit-userAgent"></h2>
  <ol id="qunit-tests"></ol>
  <br />
  <textarea id="qunit-log" rows="10" cols="100"></textarea>
  <script type="text/javascript" src="../Scripts/Sharpen.debug.js"></script>
  <script type="text/javascript">
  module('Delegates');

  test('bind', function() {
    var ctx = { value: 123 };

    var fn = ss.bind(function() { return this.value; }, ctx);
    var v = fn();

    QUnit.equal(v, 123);
  });

  test('bindWithArgs', function() {
    var ctx = { value: 123 };

    var fn = ss.bind(function(inc) { return this.value + inc; }, ctx);
    var v = fn(10);

    QUnit.equal(v, 133);
  });

  test('bindCaching', function() {
    var ctx = { value: 123 };

    var f = function() { return this.value; };
    var fn = ss.bind(f, ctx, 'xyz');
    var fn2 = ss.bind(f, ctx, 'xyz');
    
    QUnit.equal(fn, fn2);

    var v = fn();
    QUnit.equal(v, 123);
  });

  test('bindCachingWithCacheObject', function() {
    var ctx = { value: 123, $dc: { } };

    var f = function() { return this.value; };
    var fn = ss.bind(f, ctx, 'xyz');
    var fn2 = ss.bind(f, ctx, 'xyz');
    
    QUnit.equal(fn, fn2);

    var v = fn();
    QUnit.equal(v, 123);
  });

  test('bindName', function() {
    var f = function(i) { return i + 1; };
    var b = ss.bindName(f);

    QUnit.equal(typeof(window[b.name]), 'function');
    QUnit.equal(window[b.name](10), 11);
    QUnit.equal(window[b.name], undefined);

    var b2 = ss.bindName(f);
    QUnit.equal(typeof(window[b2.name]), 'function');
    b2.nop();
    QUnit.equal(typeof(window[b2.name]), 'function');
  });

  test('bindNameMultiUse', function() {
    var f = function(i) { return i + 1; };
    var b = ss.bindName(f, true);

    QUnit.equal(window[b.name], f);
    QUnit.equal(window[b.name](10), 11);

    b.dispose();
    QUnit.equal(window[b.name], undefined);

    var b2 = ss.bindName(f, true);
    QUnit.equal(window[b2.name], f);
    
    b2.nop();
    QUnit.equal(window[b2.name] != f, true);
    QUnit.equal(typeof(window[b2.name]), 'function');
  });

  test('bindNameSpecifiedName', function() {
    var f = function(i) { return i + 1; };
    var b = ss.bindName(f, false, 'foobarbaz');

    QUnit.equal(b.name, 'foobarbaz');
    QUnit.equal(typeof(window[b.name]), 'function');
    QUnit.equal(window[b.name](10), 11);
    QUnit.equal(window[b.name], undefined);
  });

  test('bindNameCustomRoot', function() {
    var o = { };
    var f = function(i) { return i + 1; };
    var b = ss.bindName(f, false, null, o);

    QUnit.equal(typeof(o[b.name]), 'function');
    QUnit.equal(o[b.name](10), 11);
    QUnit.equal(o[b.name], undefined);
  });
  
  test('delegateSingle', function() {
    QUnit.expect(2);
    var f = function() { QUnit.equal('f', 'f'); };
    
    var d;
    d = ss.delegate(d, f);
    QUnit.equal(d, f);
    d();
  });
  
  test('delegateMultiple', function() {
    var sequence = [];
    
    QUnit.expect(7);
    var f = function() { sequence.push('f'); QUnit.equal('f', 'f'); };
    var g = function() { sequence.push('g'); QUnit.equal('g', 'g'); };
    
    var d;
    d = ss.delegate(d, f);
    d = ss.delegate(d, g);
    d();
    QUnit.equal(sequence.join(), 'f,g');

    sequence = [];
    var d2 = null;
    d2 = ss.delegate(d, f);
    d2 = ss.delegate(d, g);
    d2 = ss.delegate(d, f);
    d2();
    QUnit.equal(sequence.join(), 'f,g,f');
  });
  
  test('delegateRemove', function() {
    QUnit.expect(2);
    var f = function() { QUnit.equal('f', 'f'); };
    
    var d;
    d = ss.delegate(d, f);
    QUnit.equal(d, f);
    
    d = ss.undelegate(d, f);
    QUnit.equal(d, null);
  });
  
  test('delegateMultipleRemove', function() {
    QUnit.expect(10);
    
    var sequence = [];
    var f = function() { sequence.push('f'); QUnit.equal('f', 'f'); };
    var g = function() { sequence.push('g'); QUnit.equal('g', 'g'); };
    var h = function() { sequence.push('h'); QUnit.equal('h', 'h'); };
    
    var d;
    d = ss.delegate(d, f);
    d = ss.delegate(d, g);
    d = ss.undelegate(d, f);
    d();
    QUnit.equal(sequence.join(), 'g');

    sequence = [];
    d = null;
    d = ss.delegate(d, f);
    d = ss.delegate(d, g);
    d = ss.undelegate(d, g);
    d();
    QUnit.equal(sequence.join(), 'f');

    sequence = [];
    d = null;
    d = ss.delegate(d, f);
    d = ss.delegate(d, g);
    d = ss.delegate(d, h);
    d = ss.undelegate(d, g);
    d();
    QUnit.equal(sequence.join(), 'f,h');

    sequence = [];
    d = null;
    d = ss.delegate(d, f);
    d = ss.delegate(d, g);
    d = ss.delegate(d, h);
    d = ss.undelegate(d, h);
    d();
    QUnit.equal(sequence.join(), 'f,g');
  });
  </script>
</body>

</html>
